import pandas as pd
from ListUtils import anti_join

"""
指数基本信息
"""

def insert_history_data_index_basic(engine, pro):
    """
    保存历史数据，通过对比添加数据
    """
    insert_flag = True # 添加标记
    if insert_flag:
        source_list = pro.index_basic()
        sql = 'select t.ts_code from index_basic t'
        target_list = pd.read_sql(sql, engine)

        if len(target_list) > 0:
            source_list = anti_join(source_list, target_list, ['ts_code'])
            if len(source_list) > 0:
                print('增量：(%s)' %source_list)
                source_list.to_sql('index_basic', con=engine, index=False, index_label='ts_code', if_exists='append')
        else:
            source_list.to_sql('index_basic', con=engine, index=False, index_label='ts_code', if_exists='append')
            print('数据库内未找到数据则全部添加：(%s)' % source_list)
    # 测试时间区间筛选
    # query_start = '20200101'
    # query_end = '20210101'
    # df = pd.read_sql('select t.* from trade_cal t WHERE t.cal_date > ' + query_start + ' and t.cal_date < ' + query_end , engine)
    # print(df)
